package cn.edu.scau.cmi.ema.domain;


import java.io.Serializable;

import java.lang.StringBuilder;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.persistence.*;
import javax.xml.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.*;
import java.util.Set;
import com.alibaba.fastjson.annotation.JSONField;

@Entity
@NamedQueries({ @NamedQuery(name = "findAllUnits", query = "select myUnit from Unit myUnit"),
		@NamedQuery(name = "findUnitByName", query = "select myUnit from Unit myUnit where myUnit.name = ?1"),
		@NamedQuery(name = "findUnitByNameContaining", query = "select myUnit from Unit myUnit where myUnit.name like concat('%',?1,'%')"),
		@NamedQuery(name = "findUnitByType", query = "select myUnit from Unit myUnit where myUnit.type = ?1"),
		@NamedQuery(name = "findUnitByTypeContaining", query = "select myUnit from Unit myUnit where myUnit.type like concat('%',?1,'%')"),
		@NamedQuery(name = "findUnitByDescription", query = "select myUnit from Unit myUnit where myUnit.description = ?1"),
		@NamedQuery(name = "findUnitByDescriptionContaining", query = "select myUnit from Unit myUnit where myUnit.description like concat('%',?1,'%')"),
		@NamedQuery(name = "findUnitById", query = "select myUnit from Unit myUnit where myUnit.id = ?1"),
		@NamedQuery(name = "findUnitByPrimaryKey", query = "select myUnit from Unit myUnit where myUnit.id = ?1 "),
		@NamedQuery(name = "deleteUnitsByIds", query = "delete Unit where id in (?1)"),
		@NamedQuery(name = "findUnitsByIds", query = "select myUnit from Unit myUnit where myUnit.id in (?1)"),
		@NamedQuery(name = "findUnitByPhyscialPrimaryKey", query = "select myUnit from Unit myUnit where  myUnit.name = ?1 and myUnit.type = ?2"),
		@NamedQuery(name = "countUnitsByName", query = "select count(myUnit)  from Unit myUnit where myUnit.name like concat('%',?1,'%')") })
@Table(catalog = "ema", name = "unit")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = "ema/cn/edu/scau/cmi/ema/domain", name = "Unit")
@XmlRootElement(namespace = "ema/cn/edu/scau/cmi/ema/domain")

public class Unit implements Serializable, Comparable<Unit> {

	private static final long serialVersionUID = 1L;

	@Column(name = "id", nullable = false)
	@Basic(fetch = FetchType.EAGER)
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@XmlElement
	Integer id;

	@Column(name = "name")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String name;

	@Column(name = "type")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String type;

	@Column(name = "description")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String description;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "unit", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Additiveaddress> additiveaddresssForUnit;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "unit", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Detectdetail> detectdetailsForUnit;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "unit", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Detectfeature> detectfeaturesForUnit;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "unit", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Detectstandard> detectstandardsForUnit;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "unit", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Expire> expiresForUnit;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "unit", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Featurestandard> featurestandardsForUnit;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "unit", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Foodbatch> foodbatchsForUnit;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "unit", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Foodbatchfiller> foodbatchfillersForUnit;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "unit", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Foodsaleaddress> foodsaleaddresssForUnit;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "unit", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Materialaddress> materialaddresssForUnit;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "unit", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Packingmethod> packingmethodsForUnit;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "unit", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Processmethod> processmethodsForUnit;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "unit", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Purchaseadditive> purchaseadditivesForUnit;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "unit", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Purchasematerial> purchasematerialsForUnit;

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getId() {
		return this.id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return this.name;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getType() {
		return this.type;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		return this.description;
	}

	public void setAdditiveaddresssForUnit(Set<Additiveaddress> additiveaddresssForUnit) {
		this.additiveaddresssForUnit = additiveaddresssForUnit;
	}

	public Set<Additiveaddress> getAdditiveaddresssForUnit() {
		if (additiveaddresssForUnit == null) {
			additiveaddresssForUnit = new java.util.LinkedHashSet<Additiveaddress>();
		}
		return additiveaddresssForUnit;
	}

	public void setDetectdetailsForUnit(Set<Detectdetail> detectdetailsForUnit) {
		this.detectdetailsForUnit = detectdetailsForUnit;
	}

	public Set<Detectdetail> getDetectdetailsForUnit() {
		if (detectdetailsForUnit == null) {
			detectdetailsForUnit = new java.util.LinkedHashSet<Detectdetail>();
		}
		return detectdetailsForUnit;
	}

	public void setDetectfeaturesForUnit(Set<Detectfeature> detectfeaturesForUnit) {
		this.detectfeaturesForUnit = detectfeaturesForUnit;
	}

	public Set<Detectfeature> getDetectfeaturesForUnit() {
		if (detectfeaturesForUnit == null) {
			detectfeaturesForUnit = new java.util.LinkedHashSet<Detectfeature>();
		}
		return detectfeaturesForUnit;
	}

	public void setDetectstandardsForUnit(Set<Detectstandard> detectstandardsForUnit) {
		this.detectstandardsForUnit = detectstandardsForUnit;
	}

	public Set<Detectstandard> getDetectstandardsForUnit() {
		if (detectstandardsForUnit == null) {
			detectstandardsForUnit = new java.util.LinkedHashSet<Detectstandard>();
		}
		return detectstandardsForUnit;
	}

	public void setExpiresForUnit(Set<Expire> expiresForUnit) {
		this.expiresForUnit = expiresForUnit;
	}

	public Set<Expire> getExpiresForUnit() {
		if (expiresForUnit == null) {
			expiresForUnit = new java.util.LinkedHashSet<Expire>();
		}
		return expiresForUnit;
	}

	public void setFeaturestandardsForUnit(Set<Featurestandard> featurestandardsForUnit) {
		this.featurestandardsForUnit = featurestandardsForUnit;
	}

	public Set<Featurestandard> getFeaturestandardsForUnit() {
		if (featurestandardsForUnit == null) {
			featurestandardsForUnit = new java.util.LinkedHashSet<Featurestandard>();
		}
		return featurestandardsForUnit;
	}

	public void setFoodbatchsForUnit(Set<Foodbatch> foodbatchsForUnit) {
		this.foodbatchsForUnit = foodbatchsForUnit;
	}

	public Set<Foodbatch> getFoodbatchsForUnit() {
		if (foodbatchsForUnit == null) {
			foodbatchsForUnit = new java.util.LinkedHashSet<Foodbatch>();
		}
		return foodbatchsForUnit;
	}

	public void setFoodbatchfillersForUnit(Set<Foodbatchfiller> foodbatchfillersForUnit) {
		this.foodbatchfillersForUnit = foodbatchfillersForUnit;
	}

	public Set<Foodbatchfiller> getFoodbatchfillersForUnit() {
		if (foodbatchfillersForUnit == null) {
			foodbatchfillersForUnit = new java.util.LinkedHashSet<Foodbatchfiller>();
		}
		return foodbatchfillersForUnit;
	}

	public void setFoodsaleaddresssForUnit(Set<Foodsaleaddress> foodsaleaddresssForUnit) {
		this.foodsaleaddresssForUnit = foodsaleaddresssForUnit;
	}

	public Set<Foodsaleaddress> getFoodsaleaddresssForUnit() {
		if (foodsaleaddresssForUnit == null) {
			foodsaleaddresssForUnit = new java.util.LinkedHashSet<Foodsaleaddress>();
		}
		return foodsaleaddresssForUnit;
	}

	public void setMaterialaddresssForUnit(Set<Materialaddress> materialaddresssForUnit) {
		this.materialaddresssForUnit = materialaddresssForUnit;
	}

	public Set<Materialaddress> getMaterialaddresssForUnit() {
		if (materialaddresssForUnit == null) {
			materialaddresssForUnit = new java.util.LinkedHashSet<Materialaddress>();
		}
		return materialaddresssForUnit;
	}

	public void setPackingmethodsForUnit(Set<Packingmethod> packingmethodsForUnit) {
		this.packingmethodsForUnit = packingmethodsForUnit;
	}

	public Set<Packingmethod> getPackingmethodsForUnit() {
		if (packingmethodsForUnit == null) {
			packingmethodsForUnit = new java.util.LinkedHashSet<Packingmethod>();
		}
		return packingmethodsForUnit;
	}

	public void setProcessmethodsForUnit(Set<Processmethod> processmethodsForUnit) {
		this.processmethodsForUnit = processmethodsForUnit;
	}

	public Set<Processmethod> getProcessmethodsForUnit() {
		if (processmethodsForUnit == null) {
			processmethodsForUnit = new java.util.LinkedHashSet<Processmethod>();
		}
		return processmethodsForUnit;
	}

	public void setPurchaseadditivesForUnit(Set<Purchaseadditive> purchaseadditivesForUnit) {
		this.purchaseadditivesForUnit = purchaseadditivesForUnit;
	}

	public Set<Purchaseadditive> getPurchaseadditivesForUnit() {
		if (purchaseadditivesForUnit == null) {
			purchaseadditivesForUnit = new java.util.LinkedHashSet<Purchaseadditive>();
		}
		return purchaseadditivesForUnit;
	}

	public void setPurchasematerialsForUnit(Set<Purchasematerial> purchasematerialsForUnit) {
		this.purchasematerialsForUnit = purchasematerialsForUnit;
	}

	public Set<Purchasematerial> getPurchasematerialsForUnit() {
		if (purchasematerialsForUnit == null) {
			purchasematerialsForUnit = new java.util.LinkedHashSet<Purchasematerial>();
		}
		return purchasematerialsForUnit;
	}

	public void copy(Unit that) {

		setId(that.getId());

		setName(that.getName());
		setType(that.getType());
		setDescription(that.getDescription());

		setAdditiveaddresssForUnit(new java.util.LinkedHashSet<Additiveaddress>(
				that.getAdditiveaddresssForUnit()));

		setDetectdetailsForUnit(
				new java.util.LinkedHashSet<Detectdetail>(that.getDetectdetailsForUnit()));

		setDetectfeaturesForUnit(
				new java.util.LinkedHashSet<Detectfeature>(that.getDetectfeaturesForUnit()));

		setDetectstandardsForUnit(new java.util.LinkedHashSet<Detectstandard>(
				that.getDetectstandardsForUnit()));

		setExpiresForUnit(new java.util.LinkedHashSet<Expire>(that.getExpiresForUnit()));

		setFeaturestandardsForUnit(new java.util.LinkedHashSet<Featurestandard>(
				that.getFeaturestandardsForUnit()));

		setFoodbatchsForUnit(
				new java.util.LinkedHashSet<Foodbatch>(that.getFoodbatchsForUnit()));

		setFoodbatchfillersForUnit(new java.util.LinkedHashSet<Foodbatchfiller>(
				that.getFoodbatchfillersForUnit()));

		setFoodsaleaddresssForUnit(new java.util.LinkedHashSet<Foodsaleaddress>(
				that.getFoodsaleaddresssForUnit()));

		setMaterialaddresssForUnit(new java.util.LinkedHashSet<Materialaddress>(
				that.getMaterialaddresssForUnit()));

		setPackingmethodsForUnit(
				new java.util.LinkedHashSet<Packingmethod>(that.getPackingmethodsForUnit()));

		setProcessmethodsForUnit(
				new java.util.LinkedHashSet<Processmethod>(that.getProcessmethodsForUnit()));

		setPurchaseadditivesForUnit(new java.util.LinkedHashSet<Purchaseadditive>(
				that.getPurchaseadditivesForUnit()));

		setPurchasematerialsForUnit(new java.util.LinkedHashSet<Purchasematerial>(
				that.getPurchasematerialsForUnit()));

	}

	public Unit() {

	}

	@Override
	public String toString() {

		StringBuilder buffer = new StringBuilder();

		buffer.append("id=[").append(id).append("] ");

		buffer.append("name=[").append(name).append("] ");
		buffer.append("type=[").append(type).append("] ");
		buffer.append("description=[").append(description).append("] ");

		return buffer.toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = (int) (prime * result + ((id == null) ? 0 : id.hashCode()));
		return result;
	}

	public boolean equals(Object obj) {
		if (obj == this)
			return true;
		if (!(obj instanceof Unit))
			return false;
		Unit comparedUnit = (Unit) obj;
		if (!CheckPropertyEqual(this.name, comparedUnit.name)) {
			return false;
		}
		if (!CheckPropertyEqual(this.type, comparedUnit.type)) {
			return false;
		}
		return true;
	}

	public static <T> boolean CheckPropertyEqual(T originProperty, T comparedProperty) {
		// 基本类型经过参数传递后会自动装箱
		String[] className = originProperty.getClass().getName().split("\\.");
		if (className[0].equals("java") && className[1].equals("math")) {
			return ((Comparable) originProperty).compareTo(comparedProperty) == 0 ? true : false;
		} else {
			return originProperty.equals(comparedProperty);
		}
	}

	@Override
	public int compareTo(Unit o) {
		if (this.getId() > o.getId()) {
			return 1;
		} else if (this.getId() == o.getId()) {
			return 0;
		} else {
			return -1;
		}

	}
}
